﻿using System.Collections.Generic;
using System.Reflection;
using EnvDTE;

namespace StyleCopMaker.StyleCopMaker.Cleanup
{
	public class CleanupGlobal
	{
		private static List<ICleanupCode> cleanupCodes = new List<ICleanupCode>();

		private static Dictionary<string, ICleanupCode> cleanupCodesDico = new Dictionary<string, ICleanupCode>();

		public static void Init(StyleCopMakerPackage package)
		{
			Assembly assembly = Assembly.GetExecutingAssembly();

			foreach (var item in assembly.GetTypes())
			{
				if (item.GetInterface(typeof(ICleanupCode).FullName) != null)
				{
					var c = assembly.CreateInstance(item.FullName) as ICleanupCode;
					c.Package = package;
					c.InitCleaner();
					cleanupCodes.Add(c);
					cleanupCodesDico.Add(c.DocumentType, c);
				}
			}
		}

		public static void CleanDocument(Document document)
		{
			if (document != null)
			{
				ICleanupCode c;

				if (cleanupCodesDico.TryGetValue(document.Language, out c))
				{
					c.Clean(document);
				}
			}
		}
	}
}